package com.briup.web_demo2.web.controller;


import com.briup.web_demo2.pojo.Student;
import com.briup.web_demo2.response.Response;
import com.briup.web_demo2.response.PageTemplate;
import com.briup.web_demo2.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

//解决跨域问题 (协议 主机 端口号）
@CrossOrigin
//标注在Controller类上面 给模块起名字
@Tag(name="学生信息")
@RestController
@RequestMapping("/student")
public class StudentController {

    //1.@Autowired
    @Resource(name="studentServiceImpl")
    private StudentService studentService;

    @PostMapping
    @Operation(summary = "新增学生")
    public Response insertStudent(@RequestBody Student student) {
        studentService.insertStudent(student);
        return Response.success();
    }

    @DeleteMapping("/{studentId}")
    @Operation(summary = "根据Id删除学生")
    public Response deleteStudent(@PathVariable("studentId") Integer id){
        studentService.deleteStudent(id);
        return Response.success();
    }

    @PutMapping
    @Operation(summary = "修改学生信息")
    public Response editStudent(@RequestBody Student student){
        //修改学生数据
        studentService.updateStudentById(student);
        //返回响应
        return Response.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据Id查询学生信息")
    public Response<Student> getStudent(@PathVariable("id") String id){
        //查询学生信息
        Student student = studentService.selectStudentById(id);
        //返回响应
        return Response.success(student);
    }
    //手动分页   使用路径参数 /
    @GetMapping("/basePage/{pageNum}/{pageSize}")
    @Operation(summary = "基础分页")
    //返回响应
    public Response<PageTemplate<Student>> getBasePage(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageTemplate<Student> pageTemplates=studentService.getBasePage(pageNum,pageSize);
        return Response.success(pageTemplates);
    }
    //pageHelper分页  使用？拼参
    @GetMapping("/getPage")
    @Operation(summary = "pageHelper分页")
    @Parameters(
            {
                    @Parameter(name = "pageNum",description = "当前页码"),
                    @Parameter(name = "pageSize",description = "每页大小"),
            }
    )
    public Response<PageTemplate<Student>> getPageStudent(Integer pageNum, Integer pageSize){
        PageTemplate<Student> pageTemplates=studentService.getPageStudent(pageNum,pageSize);
        return Response.success(pageTemplates);
    }

    @Operation(summary = "分页+条件查询")
    @Parameters(
            {
                    @Parameter(name="pageNum",description = "当前页码",required=true),
                    @Parameter(name="pageSize",description = "每页大小",required=true),
                    @Parameter(name="id",description = "学号",required=false),
                    @Parameter(name="name",description = "学生姓名",required=false),
                    @Parameter(name="gender",description = "性别",required=false),
                    @Parameter(name="startTime",description = "开始时间",required=false),
                    @Parameter(name="endTime",description = "结束时间",required=false),
            }
    )
    @GetMapping("/getPageStudent")
    //分页+条件查询（动态sql）
    public Response<PageTemplate<Student>> getPageStudentByCondition(
            @RequestParam(value = "pageNum",required = true) Integer pageNum,
            @RequestParam(value = "pageSize",required = true)Integer pageSize,
            @RequestParam(value = "id",required = false) Integer id,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "gender",required = false) Integer gender,
            @RequestParam(value = "startTime",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(value = "endTime",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        PageTemplate<Student> students=studentService.getPageStudentByCondition(pageSize,pageNum,id,name,gender,startTime,endTime);
        return Response.success(students);
    }
}
